package com.yellowpage.ca.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ca.data.FuncModule;
import com.yellowpage.ca.service.FuncModuleService;

public class FuncModuleAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	private List<FuncModule> funcModuleList = new ArrayList<FuncModule>();
	private List<Map> resultList = new ArrayList<Map>();

	private FuncModule funcModule;
	private FuncModuleService funcModuleService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_levelCode;	
	private String query_name;
	

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.funcModule = funcModuleService.getFuncModule(Long
					.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("levelCode", query_levelCode);		
		queryMap.put("name", query_name);		

		resultList = funcModuleService.queryFuncModuleByPage(queryMap,
				pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		funcModule.setCreateTime(new Date());
		funcModule.setCreateUser(loginUser.getUserId());
		funcModule.setLastUpdateTime(new Date());
		funcModule.setLastUpdateUser(loginUser.getUserId());	
		funcModule.setStatus("1");
		
		funcModuleService.saveFuncModule(funcModule);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (funcModule.getLevelCode().length() == 0) {
			this.addActionError("** 层级代码 不能为空 !");
		}
		if (funcModule.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}	

		this.setValidatorResult("/ca/FuncModuleAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		funcModule = funcModuleService.getFuncModule(id);

		return RESULT_EDIT;
	}

	public String update() {
		funcModule.setLastUpdateTime(new Date());
		funcModule.setLastUpdateUser(loginUser.getUserId());	

		funcModuleService.updateFuncModule(funcModule);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (funcModule.getLevelCode().length() == 0) {
			this.addActionError("** 层级代码 不能为空 !");
		}
		if (funcModule.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}		
		this.setValidatorResult("/ca/FuncModuleUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		this.funcModuleService.delete(ids);

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setFuncModule(FuncModule funcModule) {
		this.funcModule = funcModule;
	}

	public FuncModule getFuncModule() {
		return funcModule;
	}

	public void setFuncModuleService(FuncModuleService funcModuleService) {
		this.funcModuleService = funcModuleService;
	}

	public List<FuncModule> getFuncModuleList() {
		return funcModuleList;
	}

	public void setFuncModuleList(List<FuncModule> funcModuleList) {
		this.funcModuleList = funcModuleList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	

	public String getQuery_levelCode() {
		return this.query_levelCode;
	}

	public void setQuery_levelCode(String levelCode) {
		this.query_levelCode = levelCode;
	}

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}
	

}
